import React from 'react';
import Ex from "ex";
import {ExAccount} from 'ei';
import Ux from "ux";
import {Col, Row} from "antd";
import './Cab.less';
import Rdr from './Web';
import Rdr2 from './Web2';
import Rdr3 from './Web3';
import Rdr4 from './Web4';

const componentInit = (reference) => {
    Ex.yiAssist(reference).then(state => {
        const {$app} = reference.props;
        const sigma = $app._("sigma");

        const loginUser = Ux.isLogged();
        console.log("loginUser")
        console.log(loginUser)
        Ux.ajaxPost("/api/xc/ci.device/search").then(response => {
            // console.log("assets");
            // console.log(response);
            const $device = Ux.valueArray(response);
            state.$cidevice = $device;
            return Ux.promise(state);
        }).then(state => {
            const usedata = {};
            const assData = {};
            const promises = [];
            state.$cidevice.forEach(citemp => {
                console.log(citemp)
                // 使用人
                if(citemp['userId'] == loginUser['workNumber']) {
                    let useNum = usedata[citemp['categoryThird']] ? usedata[citemp['categoryThird']] : 0;
                    useNum += 1;
                    usedata[citemp['categoryThird']] = useNum;
                }
                if(citemp['ownerId'] == loginUser['workNumber']) {
                    let assigneNum = assData[citemp['categoryThird']] ? assData[citemp['categoryThird']] : 0;
                    assigneNum += 1;
                    assData[citemp['categoryThird']] = assigneNum;
                }
            });

            const category = Ux.onDatum({state}, "data.category");
            const categorys = {}
            category.forEach(cat => {
                categorys[cat['key']] = cat['name'];
            })

            const ciUse = {data: [], config: {}};
            for(var i in usedata){
                const item = {};
                item.key = i;
                item.name = categorys[i];
                item.value = usedata[i];
                ciUse.data.push(item);
            }

            const ciAss = {data: [], config: {}};
            for(var i in assData){
                const item = {};
                item.key = i;
                item.name = categorys[i];
                item.value = assData[i];
                ciAss.data.push(item);
            }

            state.$ciUse = ciUse;
            state.$ciAss = ciAss;
            return Ux.promise(state);
        }).then(Ux.ready).then(Ux.pipe(reference));

        Ux.ajaxPost("/api/up/report/list",{criteria:{sigma}}).then(response => {
            // console.log(response)
            const $data = Ux.valueArray(response);
            state.$data = $data;
            return Ux.promise(state);
        }).then(state => {
            const data = {create: {}, hand: {}};
            const promises = [];
            for(var i in state.$data) {
                const temp = state.$data[i];
                const category = temp['catalog'];

                //本人提交
                if (temp['createdBy'] == loginUser['key']) {
                    const tempCreate = data['create'][temp['catalog']] ? data['create'][temp['catalog']] : [];
                    if(temp['traceId'].indexOf(tempCreate) <= 0){
                        tempCreate.push(temp['traceId']);
                    }
                    data['create'][temp['catalog']] = tempCreate;
                }
                //本人处理
                promises.push(
                    Ux.ajaxPost("/api/up/report/activity", {
                        key: temp['key'],
                        modelKey: temp['traceId'],
                        user: loginUser
                    }).then(result => {
                        var tempResult = {create: null, hand: null, other: null, user: loginUser}
                        result.forEach(activity => {
                            if (activity['createdBy'] == loginUser['key'] && tempResult.hand == null) {
                                tempResult.hand=temp;
                            }
                        })
                        return tempResult;
                    })
                )
            }
            return Ux.parallel(promises).then(result => {
                console.log(result)
                result.forEach(each => {
                    console.log(each)
                    const handTicket = each.hand;
                    if (handTicket != null) {
                        const tempHand = data['hand'][handTicket['catalog']] ? data['hand'][handTicket['catalog']] : [];
                        if(handTicket['traceId'].indexOf(tempHand) <= 0){
                            tempHand.push(handTicket['traceId']);
                        }
                        data['hand'][handTicket['catalog']] = tempHand;
                    }
                })
                // console.log(data)

                const catalog = Ux.onDatum({state}, "service.catalog");

                const submit = {data: [], config: {}};
                const hand = {data: [], config: {}};
                catalog.forEach(each => {
                    console.log(each.code)
                    const sitem = {};
                    sitem.key = each.code;
                    sitem.name = each.name;
                    sitem.value = data.create[each.code] ? Ux.valueArray(data.create[each.code]).length : 0;
                    // console.log(Ux.valueArray(data.create[each.code]).length)
                    if(sitem.value != 0) {
                        submit.data.push(sitem);
                    }
                    const hitem = {};
                    hitem.key = each.code;
                    hitem.name = each.name;
                    hitem.value = data.hand[each.code] ? Ux.valueArray(data.hand[each.code]).length : 0;
                    if(hitem.value != 0) {
                        hand.data.push(hitem);
                    }
                })
                // console.log(hand)
                state.$submit = submit;
                state.$hand = hand;
                return Ux.promise(state);
            }).then(Ux.ready).then(Ux.pipe(reference));
        })
    });
}

@Ux.zero(Ux.rxEtat(require("./Cab"))
    .cab("UI")
    .to()
)
class Component extends React.PureComponent {
    componentDidMount() {
        componentInit(this)
    }

    render() {
        return Ex.ylCard(this, () => {
            const inherit = Ex.yoAmbient(this);
            inherit.config = {};
            return (
                <div className={"ex-user-report"}>
                    {/*<Row>*/}
                    {/*    <Col span={24}>*/}
                    {/*        {Rdr3.renderStatistic(this)}*/}
                    {/*    </Col>*/}
                    {/*</Row>*/}
                    {/*<Row>*/}
                    {/*    <Col span={24}>*/}
                    {/*        {Rdr3.renderTable(this)}*/}
                    {/*    </Col>*/}
                    {/*</Row>*/}
                    <Row>
                        {/*<Col span={4}>*/}
                        {/*    <ExAccount/>*/}
                        {/*</Col>*/}
                        <Col span={12}>
                            <Row className={"rpt-head"}>本年度本人提交的各流程工单数量统计</Row>
                            <Row className={"g2-bar"}>
                                <Col span={24}>
                                    {Rdr3.renderBar(this)}
                                </Col>
                            </Row>
                        </Col>
                        <Col span={12}>
                            <Row className={"rpt-head"}>本年度本人处理的各流程工单数量统计</Row>
                            <Row className={"g2-bar"}>
                                <Col span={24}>
                                    {Rdr4.renderBar(this)}
                                </Col>
                            </Row>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={12}>
                            <Row className={"rpt-head"}>本人拥有的资产数量统计</Row>
                            <Row className={"g2-bar"}>
                                <Col span={24}>
                                    {Rdr.renderBar(this)}
                                </Col>
                            </Row>
                        </Col>
                        <Col span={12}>
                            <Row className={"rpt-head"}>本人使用的资产数量统计</Row>
                            <Row className={"g2-bar"}>
                                <Col span={24}>
                                    {Rdr2.renderBar(this)}
                                </Col>
                            </Row>
                        </Col>
                    </Row>
                </div>
            )
        }, Ex.parserOfColor("PxPending").page());
    }
}

export default Component